package com.ruoyi.common.core.page;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.RequestGetBodyUtils;
import com.ruoyi.common.utils.ServletUtils;

/**
 * 表格数据处理
 *
 * @author ruoyi
 */
public class TableSupport
{

    /**
     * 封装分页对象
     */
    public static PageDomain getPageDomain()
    {
        PageDomain pageDomain = new PageDomain();
        //格式化request中json格式
        JSONObject pageJson= RequestGetBodyUtils.formatPageJson(RequestGetBodyUtils.getBody());
        pageDomain.setPageNum(ServletUtils.getParameterToInt(Constants.PAGE_NUM)==null?pageJson.getInteger(Constants.PAGE_NUM):ServletUtils.getParameterToInt(Constants.PAGE_NUM));
        pageDomain.setPageSize(ServletUtils.getParameterToInt(Constants.PAGE_SIZE)==null?pageJson.getInteger(Constants.PAGE_SIZE):ServletUtils.getParameterToInt(Constants.PAGE_SIZE));
        pageDomain.setOrderByColumn(ServletUtils.getParameter(Constants.ORDER_BY_COLUMN)==null?pageJson.getString(Constants.ORDER_BY_COLUMN):ServletUtils.getParameter(Constants.ORDER_BY_COLUMN));
        pageDomain.setIsAsc(ServletUtils.getParameter(Constants.IS_ASC)==null?pageJson.getString(Constants.IS_ASC):ServletUtils.getParameter(Constants.IS_ASC));
        if(pageJson.containsKey(Constants.COUNT_FLAG) && false == pageJson.getBoolean(Constants.COUNT_FLAG)){
            pageDomain.setReasonable(false);
        }
        return pageDomain;
    }

    public static PageDomain buildPageRequest()
    {
        return getPageDomain();
    }
}
